﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBTool
{
    using SELECTED_PATH_FOLDER = System.String;
    using SELECTED_FILE = System.String;

    public partial class Form1 : Form
    {
        private ParsingManager _parseManager;
        private Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > _selectedFileDic;
        private SvnManager _svnManager;

        private string _userID;
        private string _userPassword;

        public Form1()
        {
            InitializeComponent();
            DefaultValueSetting();

            _userID = "dm_game";
            _userPassword = "dm_game";

            _parseManager = new ParsingManager();
            _selectedFileDic = new Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > ();
            _svnManager = new SvnManager();
        }

        private void DefaultValueSetting()
        {
            dbIP_textBox.Text = "10.30.24.12";
            dbPort_textBox.Text = "3306";
            dbName_textBox.Text = "dm_script";
            parsingTime_label.Text = "";
        }

        private void AllFile_Button_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog openTest = new FolderBrowserDialog();
            openTest.ShowDialog();

            string selectedPathFolder = openTest.SelectedPath;

            if( true == String.IsNullOrEmpty( selectedPathFolder ) )
                return;

            string[] fileList = Directory.GetFiles(openTest.SelectedPath, "*.*", SearchOption.TopDirectoryOnly);

            List<string> loadFileList = new List<string>();

            foreach( string filePath in fileList )
            {
                if( true == loadFileList.Contains( filePath ) )
                    continue;

                if( Config._exportFileExt != Path.GetExtension( filePath ) )
                    continue;

                loadFileList.Add( filePath );
            }

            if( false == _selectedFileDic.ContainsKey( selectedPathFolder ) )
                _selectedFileDic.Add( selectedPathFolder, loadFileList );
            else
                _selectedFileDic[selectedPathFolder].AddRange( loadFileList );

            LoadFileGridViewSetting();
        }

        private void SelectFile_button_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Title = "파일 선택";
            open.Multiselect = true;
            open.Filter = "CSV Files (.csv)|*.csv|All Files (*.*)|*.*";
            open.InitialDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
            
            if (open.ShowDialog() == DialogResult.OK)
            {
                string selectedPathFolder = Path.GetDirectoryName( open.FileName );

                string[] fileList = open.FileNames;

                List<string> loadFileList = new List<string>();

                foreach( string filePath in fileList )
                {
                    if( true == loadFileList.Contains( filePath ) )
                        continue;

                    if( Config._exportFileExt != Path.GetExtension( filePath ) )
                        continue;

                    loadFileList.Add( filePath );
                }

                if( false == _selectedFileDic.ContainsKey( selectedPathFolder ) )
                    _selectedFileDic.Add( selectedPathFolder, loadFileList );
                else
                    _selectedFileDic[selectedPathFolder].AddRange( loadFileList );

                LoadFileGridViewSetting();
            }
        }
        
        private void Clear_Click(object sender, EventArgs e)
        {
            _selectedFileDic.Clear();

            LoadFileGridViewSetting();
        }

        private void LoadFileGridViewSetting()
        {
            if (null == _selectedFileDic)
                return;

            this.LoadFileList_listBox.Items.Clear();

            foreach( KeyValuePair< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > file in _selectedFileDic )
            {
                foreach( string selectedFile in file.Value )
                this.LoadFileList_listBox.Items.Add( Path.GetFileName( selectedFile ) );
            }
        }
        
        private void DBConnect_Button_Click(object sender, EventArgs e)
        {
            Config._useDBParsing = true;

            Parsing();
        }

        private void OnlyTableParsing_Click(object sender, EventArgs e)
        {
            Config._useDBParsing = false;

            Parsing();
        }

        private void Parsing()
        {
            DateTime parsingStartTime = DateTime.Now;

            if (true == string.IsNullOrEmpty(dbIP_textBox.Text))
            {
                MessageBox.Show( "IP Is Null", "IP Check", MessageBoxButtons.OK, MessageBoxIcon.Error );
                return;
            }

            if (true == string.IsNullOrEmpty(dbName_textBox.Text))
            {
                MessageBox.Show("dbName Is Null", "DBName Check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (null == _selectedFileDic)
            {
                MessageBox.Show("File Is Null", "Selected File Check", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DBConnectInfo dbconnectInfo = new DBConnectInfo( dbIP_textBox.Text, dbName_textBox.Text, _userID, _userPassword );
            
            _parseManager.Parse( dbconnectInfo, _selectedFileDic );
            
            _svnManager.CommitParsedFile( _selectedFileDic );

            this.parsingTime_label.Text = ( (TimeSpan)( DateTime.Now - parsingStartTime ) ).TotalSeconds.ToString();

            MessageBox.Show( "Parsing End.!!" );
        }
    }
}
